﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DPFP;
using DPFP.Capture;
using DPFP.Error;
using DPFP.Gui;
using DPFP.Processing;
using DPFP.Verification;
using System.Windows;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;

namespace IngressApp
{

    public class Huella : UserControl, DPFP.Capture.EventHandler 
    {
        Enrollment Enroller = new Enrollment();
        private DPFP.Capture.Capture Capturer;
        byte[] Res;

        public delegate void OnTemplateEventHandler(Template template);
        public event OnTemplateEventHandler OnTemplate;
        
        
        #region suma de todo el poder


        protected DPFP.FeatureSet ExtractFeatures(DPFP.Sample Sample, DPFP.Processing.DataPurpose Purpose)
        {
            DPFP.Processing.FeatureExtraction Extractor = new DPFP.Processing.FeatureExtraction();	// Create a feature extractor
            DPFP.Capture.CaptureFeedback feedback = DPFP.Capture.CaptureFeedback.None;
            DPFP.FeatureSet features = new DPFP.FeatureSet();
            Extractor.CreateFeatureSet(Sample, Purpose, ref feedback, ref features);			// TODO: return features as a result?
            if (feedback == DPFP.Capture.CaptureFeedback.Good)
                return features;
            else
                return null;
        }
        #endregion

        #region Event handlers 

        public void OnComplete(object Capture, string ReaderSerialNumber, Sample Sample)
        {
            
            
            Process(Sample);
        }

        public void OnFingerGone(object Capture, string ReaderSerialNumber)
        {
            MessageBox.Show("OnFingerGone");
        }

        public void OnFingerTouch(object Capture, string ReaderSerialNumber)
        {
            MessageBox.Show("OnFingerTouch");
        }

        public void OnReaderConnect(object Capture, string ReaderSerialNumber)
        {
            MessageBox.Show("OnReaderConnect");
        }

        public void OnReaderDisconnect(object Capture, string ReaderSerialNumber)
        {
            MessageBox.Show("OnReaderDisconnect");
        }

        public void OnSampleQuality(object Capture, string ReaderSerialNumber, CaptureFeedback CaptureFeedback)
        {
            MessageBox.Show("OnSampleQuality");
        }
        #endregion

        public void Init() 
        {
            Enroller = new DPFP.Processing.Enrollment();			// Create an enrollment.
			UpdateStatus();

            try
            {
                Capturer = new DPFP.Capture.Capture();				// Create a capture operation.

                if (null != Capturer)
                    Capturer.EventHandler = this;					// Subscribe for capturing events.
                else
                    MessageBox.Show("Can't initiate capture operation!");
            }
            catch
            {
                MessageBox.Show("Can't initiate capture operation!");
            }

            Start();
            
        }

        protected void Start()
        {
            if (null != Capturer)
            {
                try
                {
                    
                    Capturer.StartCapture();
                    MessageBox.Show("Using the fingerprint reader, scan your fingerprint.");
                }
                catch
                {
                    MessageBox.Show("Can't initiate capture!");
                }
            }
        }

        protected void Stop()
        {
            if (null != Capturer)
            {
                try
                {
                    Capturer.StopCapture();
                }
                catch
                {
                    MessageBox.Show("Can't terminate capture!");
                }
            }
        }

            protected void Process(DPFP.Sample Sample)
            {
          
            // Process the sample and create a feature set for the enrollment purpose.
            DPFP.FeatureSet features = ExtractFeatures(Sample, DPFP.Processing.DataPurpose.Enrollment);

            // Check quality of the sample and add to enroller if it's good
            if (features != null) try
                {
                    MessageBox.Show("The fingerprint feature set was created.");
                    Enroller.AddFeatures(features);		// Add feature set to template.
                }
                finally
                {
                    UpdateStatus();

                    // Check if template has been created.
                    switch (Enroller.TemplateStatus)
                    {
                        case DPFP.Processing.Enrollment.Status.Ready:	// report success and stop capturing
                            //Enroller.Template;
                            Enroller.Template.Serialize(ref editEmployees.FPVarByte);
                            MessageBox.Show("Click Close, and then click Fingerprint Verification.");
                            Stop();
                            break;

                        case DPFP.Processing.Enrollment.Status.Failed:	// report failure and restart capturing
                            Enroller.Clear();
                            Stop();

                            UpdateStatus();
                            OnTemplate(null);
                            Start();
                            break;
                    }
                }
        }

        private void UpdateStatus()
        {
            // Show number of samples needed.
            MessageBox.Show(String.Format("Fingerprint samples needed: {0}", Enroller.FeaturesNeeded));
        }

    }
}
